/**
 *
 * @author 
 *
 */

class DragObject extends eui.Component {
    private offsetX: number = 0;
    private offsetY: number = 0;
    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }

    public onAddToStage(event: egret.Event) {
        this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);

    }

    public setDragObject() {
        //this.dragObject =  obj;
        this.start(0, 0);
    }

    /*
     * 开始拖拽
     * @param _dragObject 拖拽对象
     * @param offsetX     X轴偏移
     * @param offsetY     Y轴偏移
     * */
    public start(offsetX: number = 0, offsetY: number = 0) {
        this.offsetX = offsetX;
        this.offsetY = offsetY;
        //this.dragObject = _dragObject;
        this.touchEnabled = true;
        this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this);
    }



    public selectdragObject: eui.Component;
    public initX: number = 0;
    public initY: number = 0;
    private onTouchBegin(e: egret.TouchEvent) {

        this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this);
        this.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEend, this);
        //this.newdragObject = new egret.Bitmap(RES.getRes(this.url));
        //this.newdragObject = ToolUtils.createBitmapByName(this.imgurl);

        //this.parent.parent.setChildIndex(this.newdragObject,this.parent.parent.numChildren-1);
        this.selectdragObject = e.currentTarget as eui.Component;
        this.selectdragObject.parent.setChildIndex(this.selectdragObject, this.selectdragObject.parent.numChildren - 1);


        // this.selectdragObject.x = e.stageX -
        //     ((this.selectdragObject.width * this.selectdragObject.scaleX) / 2)
        //     - (this.selectdragObject.anchorOffsetX * (this.selectdragObject.width * this.selectdragObject.scaleX)) + this.offsetX;
        // this.selectdragObject.y = e.stageY -
        //     ((this.selectdragObject.height * this.selectdragObject.scaleY) / 2) +
        //     (this.selectdragObject.anchorOffsetY * (this.selectdragObject.height * this.selectdragObject.scaleY)) + this.offsetY;
        this.initStageX = e.stageX;
        this.initStageY = e.stageY;
        this.initDragX = this.selectdragObject.x;
        this.initDragY = this.selectdragObject.y;
        console.log( "Begin....................."+"this.initStageX:"+this.initStageY+"this.initStageY:"+this.initStageX+"this.initDragX:"+this.initDragX+"this.initDragY:"+this.initDragY);
        this.dispatchEvent(new egret.Event("dragBegin", true, false));

    }
    private initStageX:number;
    private initStageY:number;

     private initDragX:number;
    private initDragY:number;
    

    private onTouchMove(e: egret.TouchEvent) {
        console.log( " Move....................."+"e.stageX:"+e.stageX+"e.stageY:"+e.stageY);
        
        //if(this.parent.parent.contains(this.newdragObject) == false)
        //    this.parent.parent.addChildAt(this.newdragObject,this.parent.parent.numChildren - 1);
        if (this.selectdragObject) {
            this.selectdragObject.x = (e.stageX - this.initStageX)+this.initDragX;
            this.selectdragObject.y = (e.stageY - this.initStageY)+this.initDragY;
                

            this.dispatchEvent(new egret.Event("dragMove", true, false));

        }
        else {
            this.stop();
        }
    }
    private onTouchEend(e: egret.TouchEvent) {
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this);
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEend, this);

        //CardMainUI.me.dropDown(this,e.stageX,e.stageY);
        var data: any = new Object();
        data.stageX = e.stageX;
        data.stageY = e.stageY;
        data.dragObject = this;
        data.initX = this.initX;
        data.initY = this.initY;
        this.dispatchEvent(new egret.Event("dragDown", true, false, data));

    }
    public stop() {
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this);
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEend, this);
        this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this);
    }
}